﻿using Cinch;
using FR.SAFTAnalyser.Services;
using MEFedMVVM.Common;
using MEFedMVVM.ViewModelLocator;
using System.ComponentModel.Composition;
using System.Deployment.Application;

namespace FR.SAFTAnalyser.ViewModels
{
    [ExportViewModel("AboutViewModel")]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class AboutViewModel : ViewModelBase
    {
        public AboutViewModel()
        {
            if (!Designer.IsInDesignMode)
            {
                this.DisplayName = "Sobre";

                Mediator.Instance.Register(this);

                DoOpenMainWebSiteCommand = new SimpleCommand<object, object>(CanOpenMainWebSite, OpenMainWebSite);
                DoOpenSaftWebSiteCommand = new SimpleCommand<object, object>(CanOpenSaftWebSite, OpenSaftWebSite);
                DoOpenPortalFinancasCommand = new SimpleCommand<object, object>(CanOpenPortalFinancas, OpenPortalFinancas);
                DoCheckUpdatesCommand = new SimpleCommand<object, object>(CanCheckUpdates, CheckUpdates);

                if (ApplicationDeployment.IsNetworkDeployed)
                {
                    ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;

                    //actualizar o texto com a versão
                    this.Version = ad.CurrentVersion.ToString();
                }
                else
                    this.Version = "Offline";
            }
        }

        #region Binding Properties
        string version;
        public string Version
        {
            get { return version; }
            set
            {
                version = value;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<AboutViewModel>(x => x.Version));
            }
        }

        bool isCheckingForUpdate;
        public bool IsCheckingForUpdate
        {
            get { return isCheckingForUpdate; }
            set
            {
                isCheckingForUpdate = value;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<AboutViewModel>(x => x.IsCheckingForUpdate));
            }
        }

        string updateStatus;
        public string UpdateStatus
        {
            get { return updateStatus; }
            set
            {
                updateStatus = value;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<AboutViewModel>(x => x.UpdateStatus));
            }
        }

        string userUpdateAction;
        public string UserUpdateAction
        {
            get
            {
                if (string.IsNullOrEmpty(userUpdateAction))
                    userUpdateAction = UpdateAction.FIND_UPDATES;
                return userUpdateAction;
            }
            set
            {
                userUpdateAction = value;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<AboutViewModel>(x => x.UserUpdateAction));
            }
        }

        #endregion Binding Properties

        #region Message mediator

        [MediatorMessageSink(MessageType.UPDATE_AVAILABLE)]
        private void UpdateAvailable(bool updateAvailable)
        {
            IsCheckingForUpdate = false;
            if (updateAvailable)
                UserUpdateAction = UpdateAction.DOWNLOAD_UPDATE;
        }

        [MediatorMessageSink(MessageType.UPDATE_READY)]
        private void UpdateReady(bool updateReady)
        {
            UserUpdateAction = UpdateAction.RESTART_APP;
        }

        [MediatorMessageSink(MessageType.UPDATE_STATUS)]
        private void UpdateStatusMessage(string message)
        {
            UpdateStatus = message;
        }

        #endregion Message mediator

        #region Commands
        public SimpleCommand<object, object> DoOpenMainWebSiteCommand { get; private set; }
        private void OpenMainWebSite(object args)
        {
            System.Diagnostics.Process.Start(@"http://saftpt.codeplex.com/");
        }

        private bool CanOpenMainWebSite(object args)
        {
            return true;
        }

        public SimpleCommand<object, object> DoOpenSaftWebSiteCommand { get; private set; }
        private void OpenSaftWebSite(object args)
        {
            System.Diagnostics.Process.Start(@"http://www.saftpt.com/");
        }

        private bool CanOpenSaftWebSite(object args)
        {
            return true;
        }

        public SimpleCommand<object, object> DoOpenPortalFinancasCommand { get; private set; }
        private void OpenPortalFinancas(object args)
        {
            System.Diagnostics.Process.Start(@"http://info.portaldasfinancas.gov.pt/pt/apoio_contribuinte/NEWS_SAF-T_PT.htm");
        }

        private bool CanOpenPortalFinancas(object args)
        {
            return true;
        }

        public SimpleCommand<object, object> DoCheckUpdatesCommand { get; private set; }
        private void CheckUpdates(object args)
        {
            IsCheckingForUpdate = true;
            Services.SoftwareUpdate su = new Services.SoftwareUpdate();

            switch (UserUpdateAction)
            {
                case UpdateAction.FIND_UPDATES:
                    su.FindUpdates();
                    break;
                case UpdateAction.DOWNLOAD_UPDATE:
                    su.BeginUpdate();
                    break;
                case UpdateAction.RESTART_APP:
                    su.RestartApp();
                    break;
                default:
                    break;
            }
        }

        private bool CanCheckUpdates(object args)
        {
            return ApplicationDeployment.IsNetworkDeployed;
        }
        #endregion Commands
    }
}
